import static org.junit.Assert.assertEquals;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.Before;
import org.junit.Test;

public class ImageProcessorTest {
	private ImplementImageProcessor imageProcessor;
	private Image standardImg1;
	private Image standardImg2;
	private String filePath = "./bmptest/";
	
	@Before	
	public void setUp() throws Exception {
		imageProcessor = new ImplementImageProcessor();
	    standardImg1 = ImageIO.read(new File(filePath + "1.bmp"));
	    standardImg2 = ImageIO.read(new File(filePath + "2.bmp"));
	}
	
	@Test
	public void testShowChanelR() throws IOException {
		Image standard1 = ImageIO.read(new File(filePath + "goal/1_red_goal.bmp"));
		Image result1 = imageProcessor.showChanelR(standardImg1);
		assertEquals(true, compare(result1, standard1));
		
		Image standard2 = ImageIO.read(new File(filePath + "goal/2_red_goal.bmp"));
		Image result2 = imageProcessor.showChanelR(standardImg2);
		assertEquals(true, compare(result2, standard2));
	}
	
	@Test
	public void testShowChanelG() throws IOException {
		Image standard1 = ImageIO.read(new File(filePath + "goal/1_green_goal.bmp"));
		Image result1 = imageProcessor.showChanelG(standardImg1);
		assertEquals(true, compare(result1, standard1));
		
		Image standard2 = ImageIO.read(new File(filePath + "goal/2_green_goal.bmp"));
		Image result2 = imageProcessor.showChanelG(standardImg2);
		assertEquals(true, compare(result2, standard2));
	}
	
	@Test
	public void testShowChanelB() throws IOException {
		Image standard1 = ImageIO.read(new File(filePath + "goal/1_blue_goal.bmp"));
		Image result1 = imageProcessor.showChanelB(standardImg1);
		assertEquals(true, compare(result1, standard1));
		
		Image standard2 = ImageIO.read(new File(filePath + "goal/2_blue_goal.bmp"));
		Image result2 = imageProcessor.showChanelB(standardImg2);
		assertEquals(true, compare(result2, standard2));
	}
	
	@Test
	public void testShowGray() throws IOException {
		Image standard1 = ImageIO.read(new File(filePath + "goal/1_gray_goal.bmp"));
		Image result1 = imageProcessor.showGray(standardImg1);
		assertEquals(true, compare(result1, standard1));
		
		Image standard2 = ImageIO.read(new File(filePath + "goal/2_gray_goal.bmp"));
		Image result2 = imageProcessor.showGray(standardImg2);
		assertEquals(true, compare(result2, standard2));
	}
	
	
	public boolean compare(Image img1, Image img2) {
		BufferedImage buffer1 = getBuffer(img1);
	    BufferedImage buffer2 = getBuffer(img2);
	    
	    if (img1.getHeight(null) != img2.getHeight(null) || img1.getWidth(null) != img2.getWidth(null)) {
	    	return false;
	    }
	    
	    for (int row = 0; row < img1.getHeight(null); row++) {
	    	for (int col = 0; col < img1.getWidth(null); col++) {
	    		if (buffer1.getRGB(col, row) != buffer2.getRGB(col, row)) {
	    			return false;
	    		}
	    	}
	    }
	    return true;
	}
	
	public BufferedImage getBuffer(Image img) {
	    BufferedImage buffer = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_RGB);
	    Graphics2D graph = buffer.createGraphics();
	    graph.drawImage(img, 0, 0, null);
	    return buffer;
	}
	
}
